package cn.gson.DZK.dao;

import java.util.Iterator;
import java.util.Map;

import cn.gson.BaseDao;
import cn.gson.FillData;
import cn.gson.entity.Goods;
/**
 *商品管理Dao
 * */
public class GoodsDao extends BaseDao{
	// 内部类:重写填充接口方法
		FillData<Goods> fillData = new FillData<Goods>() {

			@Override
			public Goods fillData(Map<String, Object> map) {
				// 创建实体类对象
				Goods a = new Goods();
				// 取map中的每一个值 ,存放到实体对象中
				Iterator<String> it = map.keySet().iterator();
				while (it.hasNext()) {
					String x = it.next();// 取键:列名
					Object y = map.get(x);// 取值;
					if (x.equalsIgnoreCase("goodsId")) {
						a.setGoodsId((String) y);
					} else if (x.equalsIgnoreCase("goodsName")) {
						a.setGoodsName((String) y);
					} else if (x.equalsIgnoreCase("goodsDetail")) {
						a.setGoodsDetail((String) y);
					} else if (x.equalsIgnoreCase("zhuangtai")) {
						a.setZhuangtai((String) y);
					} else if (x.equalsIgnoreCase("goodsNum")) {
						a.setGoodsNum((String) y);
					}
				}
				return a;
			}

		};
		/**
		 * 添加商品、商品信息
		 * @throws Exception 
		 * */
		public boolean addGoods(Goods gd) throws Exception{
			String sql="";
			try {
				int row = this.excuteUpdate(sql, gd.getGoodsId(),
						gd.getGoodsName(),
						gd.getGoodsNum(),
						gd.getGoodsDetail(),
						gd.getZhuangtai());
				return row>0?true:false;
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("添加商品失败!");
			}
		}
		/**
		 *修改商品信息
		 * @throws Exception 
		 * */
		public boolean changeGoods(Goods gd) throws Exception{
			String sql="";
			int row;
			try {
				row = this.excuteUpdate(sql, gd.getGoodsName(),
						gd.getGoodsNum(),
						gd.getGoodsDetail(),						
						gd.getZhuangtai());
				return row>0;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new Exception("修改商品信息失败!");
			}
		}
	
}
